package com.runshu.newsmartark.util.y;

import android.content.Context;

import java.io.FileInputStream;
import java.io.FileOutputStream;


public class YFileUtils {
    //文件写入
    public static void writeFileData(String filename, String content, Context ctx) {
        try {
            FileOutputStream fos = ctx.openFileOutput( filename, Context.MODE_APPEND );//获得FileOutputStream
            //将要写入的字符串转换为byte数组
            byte[] bytes = content.getBytes();
            fos.write( bytes );//将byte数组写入文件
            fos.close();//关闭文件输出流
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //文件读取
    public static String readFileData(String fileName, Context ctx) {
        String result = "";
        try {
            FileInputStream fis = ctx.openFileInput( fileName );
            //获取文件长度
            int lenght = fis.available();
            byte[] buffer = new byte[lenght];
            fis.read( buffer );
            //将byte数组转换成指定格式的字符串
            result = new String( buffer, "UTF-8" );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
